// pages/kill/kill.js
var common = require("../../common/common.js").common;

var leader = require("../../common/leader.js").leader;

var timer = null;

const app = getApp();

Page({
    /**
   * 页面的初始数据
   */
    data: {
        choose_time: 0,
        kill_time: [],
        time_id: "",
        kill_pro: {
            products: []
        },
        kill_hour: "",
        kill_minute: "",
        kill_second: "",
        choosed_hour: "",
        myleaderinfo: [],
        page: 1
    },
    /**
   * 生命周期函数--监听页面加载
   */
    onLoad: function(options) {
        this.setData({
            domain: app.globalData.domain.slice(0, -10)
        });
        wx.showShareMenu({
            menus: [ "shareAppMessage", "shareTimeline" ]
        });
        let that = this;
        leader.getMyLeaderInfo_aync().then(() => {
            that.getKilltime();
        });
    },
    /**
   * 生命周期函数--监听页面初次渲染完成
   */
    onReady: function() {},
    /**
   * 生命周期函数--监听页面显示
   */
    onShow: function() {},
    /**
   * 生命周期函数--监听页面隐藏
   */
    onHide: function() {},
    /**
   * 生命周期函数--监听页面卸载
   */
    onUnload: function() {},
    /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
    onPullDownRefresh: function() {
        let that = this;
        that.setData({
            page: 1
        });
        leader.getMyLeaderInfo_aync().then(() => {
            that.getKilltime();
        });
        wx.stopPullDownRefresh();
    },
    /**
   * 页面上拉触底事件的处理函数
   */
    onReachBottom: function() {
        let that = this;
        that.getKillpro();
    },
    /**
   * 用户点击右上角分享
   */
    choose_time: function(e) {
        var that = this;
        var id = e.currentTarget.dataset.id;
        var time = e.currentTarget.dataset.time;
        that.setData({
            choose_time: id,
            choosed_hour: time,
            time_id: id,
            kill_hour: "",
            kill_minute: "",
            kill_second: "",
            kill_pro: {
                products: [ [] ]
            },
            page: 1
        });
        clearInterval(timer);
        that.getKillpro();
    },
    getKilltime: function() {
        var that = this;
        common.request({
            endpoint: "Seckill/show/tick-tock",
            method: "post",
            header: {},
            success: function(res) {
                let kill_list = res.extra;
                var rf_list = kill_list.filter(x => {
                    return x.flag == 1;
                });
                if (rf_list.length) {
                    that.setData({
                        kill_time: kill_list,
                        time_id: rf_list[0].id,
                        choose_time: rf_list[0].id,
                        choosed_hour: rf_list[0].time
                    });
                } else {
                    that.setData({
                        kill_time: kill_list,
                        time_id: kill_list[0].id,
                        choose_time: kill_list[0].id,
                        choosed_hour: kill_list[0].time
                    });
                }
                that.getKillpro();
            }
        });
    },
    getKillpro: function() {
        var that = this;
        var time_id = that.data.time_id;
        common.request({
            endpoint: "Seckill/show/product",
            method: "post",
            data: {
                time_id: time_id,
                leaderId: that.data.myleaderinfo.leader_id,
                page: that.data.page,
                limit: 10
            },
            header: {},
            success: function(res) {
                var kill_pro = res.extra;
                if (kill_pro.products.length > 0) {
                    kill_pro.products = that.data.page == 1 ? kill_pro.products : that.data.kill_pro.products.concat(kill_pro.products);
                    that.setData({
                        kill_pro: kill_pro,
                        page: that.data.page + 1
                    });
                }
                that.kill_countdown(res.extra.timing);
            }
        });
    },
    kill_countdown: function(times) {
        var that = this;
        timer = setInterval(function() {
            var day = 0, hour = 0, minute = 0, second = 0;
            //时间默认值
                        if (times > 0) {
                hour = Math.floor(times / (60 * 60)) - day * 24;
                minute = Math.floor(times / 60) - day * 24 * 60 - hour * 60;
                second = Math.floor(times) - day * 24 * 60 * 60 - hour * 60 * 60 - minute * 60;
            }
            if (hour <= 9) hour = "0" + hour;
            if (minute <= 9) minute = "0" + minute;
            if (second <= 9) second = "0" + second;
            that.setData({
                kill_hour: hour,
                kill_minute: minute,
                kill_second: second
            });
            times--;
        }, 1e3);
        if (times <= 0) {
            clearInterval(timer);
        }
    }
});